﻿using System;
using System.Configuration;
using System.Web.Configuration;

namespace Salient.Web.Administration.Providers
{
    public partial class chooseProviderManagement : ProvidersPage
    {
        public void Page_Load()
        {
            System.Configuration.Configuration config = OpenWebConfiguration(ApplicationPath);
            var membershipSection = (MembershipSection) config.GetSection("system.web/membership");
            var roleManagerSection = (RoleManagerSection) config.GetSection("system.web/roleManager");
            var connectionStringSection = (ConnectionStringsSection) config.GetSection("connectionStrings");

            string defaultProvider = membershipSection.DefaultProvider;
            string connectionStringName =
                membershipSection.Providers[defaultProvider].Parameters["connectionStringName"];
            bool updateText = false;
            if (defaultProvider == roleManagerSection.DefaultProvider &&
                connectionStringName == roleManagerSection.Providers[defaultProvider].Parameters["connectionStringName"])
            {
                updateText = true;
            }

            // check if providers are part of the same group.
            if (!updateText)
            {
                foreach (GroupedProperty oneGroupedProvider in GroupedProvidersList)
                {
                    if (membershipSection.DefaultProvider == oneGroupedProvider.MembershipProvider)
                    {
                        if (roleManagerSection.DefaultProvider == oneGroupedProvider.RoleProvider)
                        {
                            defaultProvider = oneGroupedProvider.Name;
                            updateText = true;
                        }
                    }
                }
            }

            if (updateText)
            {
                providerLiteral.Text = String.Format((string) GetLocalResourceObject("CurrentProvider"), defaultProvider);
            }
        }
    }
}